package com.binance.notification.web.service.abnormaltrade.integration;

import com.binance.master.models.APIRequest;
import com.binance.master.models.APIResponse;
import com.binance.master.utils.JsonUtils;
import com.binance.mbxgateway.api.IPriceApi;
import com.binance.mbxgateway.vo.request.price.GetPriceRequest;
import com.binance.notification.web.service.abnormaltrade.log.LoggerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class PriceService {
    private static final org.slf4j.Logger log = LoggerUtils.getLogger(PriceService.class);

    @Autowired
    private IPriceApi priceApi;

    public String getPriceChangePercent(String symbol) {
        log.info("PriceService.getPriceChangePercent symbol={} ", symbol);
        GetPriceRequest body = new GetPriceRequest();
        body.setSymbol(symbol);
        try {
            APIResponse<String> response = priceApi.get24hrTicker(APIRequest.instance(body));
            Map<String, String> map = JsonUtils.toMap(response.getData(), String.class, String.class);
            return map.get("priceChangePercent");
        } catch (Exception e) {
            String errorMessage = "PriceService.getPriceChangePercent symbol=" + symbol + ",exception";
            log.error(errorMessage, e);
            return null;
        }
    }

}
